package com.probox.common.entity.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.order.api.domain.OrderShipDeliverVo;
import com.probox.common.entity.domain.order.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderShipDeliverMapper;
import com.probox.common.entity.domain.order.OrderShipDeliver;
import com.probox.common.entity.service.order.IOrderShipDeliverService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单发货信息Service业务层处理
 *
 * @author hds
 * @date 2021-06-07
 */
@Service
@Transactional
public class OrderShipDeliverServiceImpl extends ServiceImpl<OrderShipDeliverMapper, OrderShipDeliver> implements IOrderShipDeliverService {
    @Autowired
    private OrderShipDeliverMapper orderShipDeliverMapper;


    /**
     * 查询订单发货信息
     *
     * @param deliverUid 订单发货信息ID
     * @return 订单发货信息
     */
    @Override
    public OrderShipDeliver selectOrderShipDeliverById(Long deliverUid) {
        return this.getById(deliverUid);
    }

    /**
     * 查询订单发货信息列表
     *
     * @param orderShipDeliver 订单发货信息
     * @return 订单发货信息
     */
    @Override
    public List<OrderShipDeliver> queryList(OrderShipDeliver orderShipDeliver) {
        QueryWrapper<OrderShipDeliver> queryWrapper = Wrappers.query();
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());

        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getDeliverUid())) && orderShipDeliver.getDeliverUid() != null) {
            queryWrapper.eq("DELIVER_UID", orderShipDeliver.getDeliverUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getOrderUid())) && orderShipDeliver.getOrderUid() != null) {
            queryWrapper.eq("ORDER_UID", orderShipDeliver.getOrderUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getOrderNo())) && orderShipDeliver.getOrderNo() != null) {
            queryWrapper.like("ORDER_NO", orderShipDeliver.getOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getDeliverType())) && orderShipDeliver.getDeliverType() != null) {
            queryWrapper.eq("DELIVER_TYPE", orderShipDeliver.getDeliverType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getCustomerUid())) && orderShipDeliver.getCustomerUid() != null) {
            queryWrapper.eq("CUSTOMER_UID", orderShipDeliver.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getCustomerName())) && orderShipDeliver.getCustomerName() != null) {
            queryWrapper.like("CUSTOMER_NAME", orderShipDeliver.getCustomerName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getAddressUid())) && orderShipDeliver.getAddressUid() != null) {
            queryWrapper.eq("ADDRESS_UID", orderShipDeliver.getAddressUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getDeliverAmount())) && orderShipDeliver.getDeliverAmount() != null) {
            queryWrapper.eq("DELIVER_AMOUNT", orderShipDeliver.getDeliverAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getDeliverMethod())) && orderShipDeliver.getDeliverMethod() != null) {
            queryWrapper.eq("DELIVER_METHOD", orderShipDeliver.getDeliverMethod());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getExpressCompany())) && orderShipDeliver.getExpressCompany() != null) {
            queryWrapper.like("EXPRESS_COMPANY", orderShipDeliver.getExpressCompany());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getExpressCode())) && orderShipDeliver.getExpressCode() != null) {
            queryWrapper.like("EXPRESS_CODE", orderShipDeliver.getExpressCode());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getExpressNo())) && orderShipDeliver.getExpressNo() != null) {
            queryWrapper.like("EXPRESS_NO", orderShipDeliver.getExpressNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getPackStatus())) && orderShipDeliver.getPackStatus() != null) {
            queryWrapper.eq("PACK_STATUS", orderShipDeliver.getPackStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipDeliver.getContent())) && orderShipDeliver.getContent() != null) {
            queryWrapper.eq("CONTENT", orderShipDeliver.getContent());
        }
        if(ObjectUtil.isNotEmpty(orderShipDeliver.getStoreId())){
            queryWrapper.last(" and order_uid in (select order_uid from order_info where order_status =1 and pay_status =1 " +
                    " and store_id = "+orderShipDeliver.getStoreId()+") order by CREATE_TIME desc ");
        }else{
            queryWrapper.orderByDesc("CREATE_TIME");
        }

        /*
        Long platFormUid = SecurityUtils.getPlatformId();
        if (platFormUid!=null && platFormUid>0)
            queryWrapper.eq("PLATFORM_UID",platFormUid);
        else
            throw new BaseException("账号异常");
        */

        return orderShipDeliverMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单发货信息
     *
     * @param orderShipDeliver 订单发货信息
     * @return 结果
     */
    @Override
    public boolean insertOrderShipDeliver(OrderShipDeliver orderShipDeliver) {

        return this.save(orderShipDeliver);

    }

    /**
     * 修改订单发货信息
     *
     * @param orderShipDeliver 订单发货信息
     * @return 结果
     */
    @Override
    public boolean updateOrderShipDeliver(OrderShipDeliver orderShipDeliver) {

        return this.updateById(orderShipDeliver);
    }

    /**
     * 批量删除订单发货信息
     *
     * @param deliverUids 需要删除的订单发货信息ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderShipDeliverByIds(Long[] deliverUids) {
        return this.removeByIds(Arrays.asList(deliverUids));
    }

    /**
     * 根据ID获取发货信息
     *
     * @param orderUid
     * @return
     */
    @Override
    public List<OrderShipDeliverVo> getByOrderUid(Long orderUid) {
        return orderShipDeliverMapper.getByOrderUid(orderUid);
    }

    /**
     * 获取发货类型为正常的发货信息
     * @param orderInfo
     * @return
     */
    @Override
    public OrderShipDeliverVo getNormalDeliver(OrderInfo orderInfo) {
        return orderShipDeliverMapper.getNormalDeliver(orderInfo);
    }


}
